package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Guard;
import cn.wolfcode.crm.query.GuardQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IGuardService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONReader;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("guard")
public class GuardController {

    @Autowired
    private IGuardService guardService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"guard:view", "保单页面"}, logical = Logical.OR)
    public String view() throws Exception {

        return "guard/guard";
    }

    @RequestMapping("stotage")
    public String stotage() throws Exception {

        return "guard/stotageList";
    }

    @RequestMapping("pending")
    public String pending() throws Exception {

        return "guard/pendingList";
    }

    @RequestMapping("checke")
    public String checke() throws Exception {

        return "guard/checkedList";
    }
    @RequestMapping("completed")
    public String complete() throws Exception {

        return "guard/completeList";
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<Guard> selectAll() throws Exception {
        return guardService.selectAll();
    }

    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) throws Exception {
        JsonResult jsonResult = new JsonResult();
        try {
            guardService.deleteByPrimaryKey(id);
        } catch (Exception e) {
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"guard:saveOrUpdate", "保单新增"}, logical = Logical.OR)
    public JsonResult saveOrUpdate(Guard entity) throws Exception {
        JsonResult jsonResult = new JsonResult();
        try {
            guardService.saveOrUpdate(entity);
        } catch (Exception e) {
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }


    @RequestMapping("storageList")
    @ResponseBody
    public PageResult list(GuardQueryObject qo) throws Exception {
        qo.setState(Guard.STORAGE_STATE);
        return guardService.query(qo);
    }

    @RequestMapping("pendingList")
    @ResponseBody
    public PageResult pendingList(GuardQueryObject qo) throws Exception {
        qo.setState(Guard.PENDING_STATE);
        return guardService.query(qo);
    }

    @RequestMapping("checked")
    @ResponseBody
    public PageResult refuseList(GuardQueryObject qo) throws Exception {
        List<Guard> guards = new ArrayList<>();
        qo.setState(Guard.REFUSE_STATE);
        PageResult query = guardService.query(qo);
        List<Guard> rows = (List<Guard>) query.getRows();
        if (rows.size()>0) {
            for (Guard row : rows) {
                guards.add(row);
            }
        }
        qo.setState(Guard.UNPAID_STATE);
        List<Guard> rows1 = (List<Guard>) guardService.query(qo).getRows();
        if (rows1.size()>0) {
            for (Guard row : rows1) {
                guards.add(row);
            }
        }
        query.setRows(guards);
        return query;
    }

    @RequestMapping("completeList")
    @ResponseBody
    public PageResult completeList(GuardQueryObject qo) throws Exception {
        qo.setState(Guard.COMPLETE_STATE);
        return guardService.query(qo);
    }

    //提审方法
    @RequestMapping("toPending")
    @ResponseBody
    public JsonResult toPending(Long id) throws Exception {
        JsonResult jsonResult = new JsonResult();
        try {
            guardService.toPending(id);
        } catch (Exception e) {
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }

    //审核被拒绝方法
    @RequestMapping("beRefused")
    @ResponseBody
    public JsonResult beRefused(Long id) throws Exception {
        JsonResult jsonResult = new JsonResult();
        try {
            guardService.beRefused(id);
        } catch (Exception e) {
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }

    //审核通过未付款
    @RequestMapping("unpaid")
    @ResponseBody
    public JsonResult unpaid(Long id) throws Exception {
        JsonResult jsonResult = new JsonResult();
        try {
            guardService.unpaid(id);
        } catch (Exception e) {
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }

    //修改状态
    @RequestMapping("updateState")
    @ResponseBody
    public JsonResult updateState(Integer state, Long id) throws Exception{
        JsonResult jsonResult = new JsonResult();
        try {
            guardService.updateState(state,id);
        }catch (Exception e){
            e.printStackTrace();
           jsonResult.mark("操作失败");
        }
        return jsonResult;
    }
}
